package com.lyf.scm.plan.domain.remote.category.facade;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.lyf.scm.common.constants.CommonConstants;
import com.lyf.scm.common.enums.ResponseMsg;
import com.lyf.scm.plan.domain.remote.category.CategoryRemote;
import com.lyf.scm.plan.domain.remote.category.dto.SkuChannelPermitDTO;
import com.lyf.scm.plan.domain.remote.category.dto.SkuPurchasePermitDTO;
import com.lyf.scm.plan.domain.remote.category.dto.SkuStorePermitDTO;
import com.lyf.scm.plan.domain.remote.category.dto.StorePackageSkuDTO;
import com.rome.arch.core.clientobject.Response;
import com.rome.arch.core.exception.RomeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 品类中心
 *
 * @author wuzhuang
 * @date 2020/08/15
 */
@Slf4j
@Service
public class CategoryFacade {

    @Resource
    private CategoryRemote categoryRemote;

    public List<SkuPurchasePermitDTO> querySkuPermit(List<String> skuCodeList) {
        log.info("查询商品采购权，参数：{}", JSON.toJSONString(skuCodeList));
        Response<List<SkuPurchasePermitDTO>> response = categoryRemote.querySkuPurchasePermit(skuCodeList);
        log.info("查询商品采购权，响应参数：{}", JSON.toJSONString(response));
        if (!ResponseMsg.SUCCESS.getCode().equals(response.getCode())) {
            throw new RomeException(ResponseMsg.EXCEPTION_2003.getCode(), ResponseMsg.EXCEPTION_2003.getMsg());
        }
        List<SkuPurchasePermitDTO> data = response.getData();
        if (CollectionUtils.isEmpty(data)) {
            throw new RomeException(ResponseMsg.EXCEPTION_2004.getCode(), ResponseMsg.EXCEPTION_2004.getMsg());
        }
        return data;
    }

    public List<StorePackageSkuDTO> queryStorePackageSku() {
        Response<PageInfo<StorePackageSkuDTO>> response = categoryRemote.queryStorePackageSku(CommonConstants.PAGE_NUM, CommonConstants.PAGE_SIZE);
        log.info("查询新开店套餐，响应参数：{}", JSON.toJSONString(response));
        if (!ResponseMsg.SUCCESS.getCode().equals(response.getCode())) {
            throw new RomeException(ResponseMsg.EXCEPTION_2003.getCode(), ResponseMsg.EXCEPTION_2003.getMsg() + "获取新开店套餐");
        }
        PageInfo<StorePackageSkuDTO> pageList = response.getData();
        List<StorePackageSkuDTO> skuList = pageList.getList();
        //每页只能取100条
        if (pageList.getTotal() > CommonConstants.PAGE_SIZE) {
            for (int i = 0; i < pageList.getTotal()/ CommonConstants.PAGE_SIZE + 1; i++) {
                skuList.addAll(categoryRemote.queryStorePackageSku(CommonConstants.PAGE_NUM + i + 1, CommonConstants.PAGE_SIZE).getData().getList());
            }
        }
        return skuList;
    }

    public List<SkuPurchasePermitDTO> queryChannelSkuPermit(SkuChannelPermitDTO skuChannelPermitDTO) {
        log.info("查询渠道商品叫货权，参数：{}", JSON.toJSONString(skuChannelPermitDTO));
        Response<List<SkuPurchasePermitDTO>> response = categoryRemote.queryChannelSkuPermit(skuChannelPermitDTO);
        log.info("查询渠道商品叫货权，响应参数：{}", JSON.toJSONString(response));
        if (!ResponseMsg.SUCCESS.getCode().equals(response.getCode())) {
            throw new RomeException(ResponseMsg.EXCEPTION_2003.getCode(), ResponseMsg.EXCEPTION_2003.getMsg() + "获取渠道商品叫货权" + skuChannelPermitDTO);
        }
        List<SkuPurchasePermitDTO> data = response.getData();
        if (CollectionUtils.isEmpty(data)) {
            throw new RomeException(ResponseMsg.EXCEPTION_2004.getCode(), ResponseMsg.EXCEPTION_2004.getMsg());
        }
        return data;
    }

    public List<SkuPurchasePermitDTO> queryStoreSkuPermit(SkuStorePermitDTO skuStorePermitDTO) {
        log.info("查询门店商品叫货权，参数：{}", JSON.toJSONString(skuStorePermitDTO));
        Response<List<SkuPurchasePermitDTO>> response = categoryRemote.queryStoreSkuPermit(skuStorePermitDTO);
        log.info("查询门店商品叫货权，响应参数：{}", JSON.toJSONString(response));
        if (!ResponseMsg.SUCCESS.getCode().equals(response.getCode())) {
            throw new RomeException(ResponseMsg.EXCEPTION_2003.getCode(), ResponseMsg.EXCEPTION_2003.getMsg() + "获取门店商品叫货权" + skuStorePermitDTO);
        }
        List<SkuPurchasePermitDTO> data = response.getData();
        if (CollectionUtils.isEmpty(data)) {
            throw new RomeException(ResponseMsg.EXCEPTION_2004.getCode(), ResponseMsg.EXCEPTION_2004.getMsg());
        }
        return data;
    }
}
